package com.chengqs.rapid.client.core.autoconfigure;

import com.chengqs.rapid.client.support.dubbo.Dubbo27ClientRegistryManager;
import com.chengqs.rapid.client.support.springmvc.SpringMvcClientRegistryManager;
import org.apache.dubbo.config.spring.ServiceBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Servlet;

/**
 * <B>主类名称：</B>RapidClientAutoConfiguration<BR>
 * <B>概要说明：</B>SpringBoot自动装配加载类<BR>
 *
 * @author: chengqs
 * @create: 2025-03-15 23:00:32
 **/
@Configuration
@EnableConfigurationProperties(RapidProperties.class)
@ConditionalOnProperty(prefix = RapidProperties.RAPID_PREFIX, name = {"registryAddress", "namespace"})
public class RapidClientAutoConfiguration {
    @Autowired
    private RapidProperties rapidProperties;

    @Bean
    @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
    @ConditionalOnMissingBean(SpringMvcClientRegistryManager.class)
    public SpringMvcClientRegistryManager springMvcClientRegistryManager() throws Exception {
        return new SpringMvcClientRegistryManager(rapidProperties);
    }

    @Bean
    @ConditionalOnClass({ServiceBean.class})
    @ConditionalOnMissingBean(Dubbo27ClientRegistryManager.class)
    public Dubbo27ClientRegistryManager dubboClientRegistryManager() throws Exception {
        return new Dubbo27ClientRegistryManager(rapidProperties);
    }
}
